//
//  UIColor+Addition.m
//  Project
//
//  Created by  CarlsonLee on 2018/12/14.
//  Copyright © 2018年 YANJING. All rights reserved.
//

#import "UIColor+Addition.h"

@implementation UIColor (Addition)

+ (UIColor *)gel_randomColor
{
    NSInteger redValue = arc4random() % 255;
    NSInteger greenValue = arc4random() % 255;
    NSInteger blueValue = arc4random() % 255;
    UIColor *randomColor = [UIColor colorWithRed:redValue / 255.0f green:greenValue / 255.0f blue:blueValue / 255.0f alpha:1.0f];
    return randomColor;
}

+ (UIColor *)gel_gradientFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor gradientStyle:(GELGradientDirectionStyle)gradientStyle withWidthOrHeight:(NSInteger)widthOrHeight
{
    CGSize size;
    if (gradientStyle == GELGradientFromTopToBotom) {
        size = CGSizeMake(1, widthOrHeight);
    }else{
        size = CGSizeMake(widthOrHeight, 1);
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSArray* colors = [NSArray arrayWithObjects:(id)fromColor.CGColor, (id)toColor.CGColor, nil];
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
    
    if (gradientStyle == GELGradientFromTopToBotom) {
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
    }else{
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size.width, 0), 0);
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];
}
@end
